<?php
/**
 * Classe para enviar e-mail
 * @author Thiago Bezerra e Renato Lenz @ PC4 Team
 */
class PC4_Util_Email{
	
	protected $config_smtp;
	protected $CI;
	
	public function __construct(){
		$this->CI =& get_instance();
		$this->CI->load->library('email');
		$this->setConfig($this->CI->config->item('smtp'));
	}
	

	/**
	 * @return the $config_smtp
	 */
	public function getConfig() {
		return $this->config_smtp;
	}

	/**
	 * @param $config_smtp the $config_smtp to set
	 */
	public function setConfig($config_smtp) {
		$this->config_smtp = $config_smtp;
	}

	/**
	 * Envia o e-mail
	 * @param string $from
	 * @param string $subject
	 * @param string $message
	 * @param string $to
	 * @param string $from
	 * @param string $fromName
	 * @param string $cc
	 * @param string $bcc
	 * @param array $anexos
	 */
	public function sendMail($subject, $message, $to, $from, $fromName = "", $cc = '', $bcc = '', $anexos = array()){
		$this->CI->email->initialize($this->getConfig());
		$this->CI->email->from($from, $fromName);
		$this->CI->email->subject($subject);
		$this->CI->email->message($message);
		$this->CI->email->to($to);
		$this->CI->email->cc($cc);
		$this->CI->email->bcc($bcc);
	
		foreach ((array)$anexos as $arquivo) {
			$this->CI->email->attach($arquivo);
		}
		$ok = $this->CI->email->send();
//d($this->CI->email->print_debugger(), $ok);
		return $ok;
	}
	
	function indica($mCrud, $id, $dadosEnvio, $view, $link = '', $assunto = '', $nome = ''){
		$rs = $mCrud->ve($id);

		$dados = array(
			'post' 	=> $dadosEnvio,
			'rs'	=> $rs,
			'link'	=> $link."/".$rs->slug
		);
		
		$conteudo = $this->CI->load->view($view, $dados, true);

		return $this->sendMail($assunto, $conteudo, $dadosEnvio['emailDestinatario'], $dadosEnvio['emailRemetente'], $nome);
	}
	
	
}